package com.smile.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.annotation.Order;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

@Configuration
@EnableWebSocketMessageBroker
@Lazy
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

//    @Override
//    public void registerStompEndpoints(StompEndpointRegistry registry) {
//        // 注册STOMP端点，允许客户端连接
//        registry.addEndpoint("/ws").setAllowedOriginPatterns("*").withSockJS();
//    }
//
//    @Override
//    public void configureMessageBroker(MessageBrokerRegistry registry) {
//        // 设置消息代理前缀
//        registry.enableSimpleBroker("/topic", "/queue","/websocket");
//        // 设置应用目的前缀
//        registry.setApplicationDestinationPrefixes("/app");
//    }

//    @Override
//    public void registerStompEndpoints(StompEndpointRegistry registry) {
//        // 注意：此端点应与前端连接的路径一致
//        registry.addEndpoint("/")
//                .setAllowedOriginPatterns("*"); // 允许跨域
//    }
//
//    @Bean
//    public ServerEndpointExporter serverEndpointExporter(){
//        return new ServerEndpointExporter();
//    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/ws") // 统一端点路径
                .setAllowedOriginPatterns("*")
                .withSockJS();     // 启用SockJS支持
    }

    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        registry.enableSimpleBroker("/topic", "/queue"); // 消息代理前缀
        registry.setApplicationDestinationPrefixes("/app"); // 应用前缀
    }

}
